import base64,json
if __name__ == '__main__':

    # 头部
    header = {"typ":"jwt","alg":"HS256"}
    header_str = base64.b64encode( json.dumps(header).encode() ).decode()

    # 载荷
    preload = {"sub": "1234567890", "exp": "150123455", "name": "John Doe", "admin": True, "acc_pwd":  "QiLCJhbGciOiJIUzI1NiJ9",}
    preload_str = base64.b64encode( json.dumps(preload).encode() ).decode()

    # 签证
    import hashlib
    from django.conf import settings
    secret = 'django-insecure-%f=&+=e8q_a=ckx&5h%rkc5rxfu54wkj=-sqf#5al46_6af&^0'
    data = header_str + preload_str + secret # 秘钥绝对不能提供给客户端。
    HS256 = hashlib.sha256()
    HS256.update( data.encode('utf-8') )
    signature = HS256.hexdigest()

    token = f"{header_str}.{preload_str}.signature"
    print(token)
    # 最终jwt就是
    # 0ccadb72a8dc3b832aa6c10befe341c31e063cd4776d12123e00a51c1963c439